package view;

import databasehandling.DatabaseHandler;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import utility.FileHandler;

public class Indstillinger extends javax.swing.JDialog {

    private final MainGUI mainGUI;

    /**
     * Opretter nyt IndstillingerPanel
     * @param parent
     * @param modal 
     */
    public Indstillinger(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        mainGUI = (MainGUI) parent;
        setSize(450, 450);
        setLocationRelativeTo(null);
        setFields();
    }

    /**
     * Metoden sætter tekstfelter med de data læst fra ini.txt filen
     */
    private void setFields() {
        try {
            ArrayList<String> stringList = FileHandler.readFile("ini.txt");
            HostTxt.setText(stringList.get(0));
            PortNrTxt.setText(stringList.get(1) + "");
            DatabaseTxt.setText(stringList.get(2));
            UsernameTxt.setText(stringList.get(3));
            passwordTxt.setText(stringList.get(4));
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "Inifilen blev ikke fundet\n" + ex.getLocalizedMessage());
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        HostLabel = new javax.swing.JLabel();
        PortNrLabel = new javax.swing.JLabel();
        DatabaseLabel = new javax.swing.JLabel();
        UsernameLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        HostTxt = new javax.swing.JTextField();
        PortNrTxt = new javax.swing.JTextField();
        DatabaseTxt = new javax.swing.JTextField();
        UsernameTxt = new javax.swing.JTextField();
        passwordTxt = new javax.swing.JTextField();
        OverskriftLabel = new javax.swing.JLabel();
        ForbindKnap = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jLabel3.setText("jLabel3");

        jTextField6.setText("jTextField6");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Indstillinger");
        setAlwaysOnTop(true);
        getContentPane().setLayout(null);

        jPanel1.setOpaque(false);

        HostLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        HostLabel.setForeground(new java.awt.Color(255, 255, 255));
        HostLabel.setText("Host/IP:");

        PortNrLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        PortNrLabel.setForeground(new java.awt.Color(255, 255, 255));
        PortNrLabel.setText("Portnr:");

        DatabaseLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        DatabaseLabel.setForeground(new java.awt.Color(255, 255, 255));
        DatabaseLabel.setText("Database:");

        UsernameLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        UsernameLabel.setForeground(new java.awt.Color(255, 255, 255));
        UsernameLabel.setText("UserName:");

        PasswordLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        PasswordLabel.setForeground(new java.awt.Color(255, 255, 255));
        PasswordLabel.setText("Password:");

        OverskriftLabel.setFont(new java.awt.Font("Garamond", 0, 36)); // NOI18N
        OverskriftLabel.setForeground(new java.awt.Color(255, 255, 255));
        OverskriftLabel.setText("Database login");

        ForbindKnap.setText("Forbind");
        ForbindKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ForbindKnapActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(OverskriftLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PortNrLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(HostLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DatabaseLabel)
                            .addComponent(UsernameLabel)
                            .addComponent(PasswordLabel))
                        .addGap(69, 69, 69)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(passwordTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(UsernameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DatabaseTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(HostTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(PortNrTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(148, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ForbindKnap)
                .addGap(58, 58, 58))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(OverskriftLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HostLabel)
                    .addComponent(HostTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PortNrLabel)
                    .addComponent(PortNrTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DatabaseLabel)
                    .addComponent(DatabaseTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UsernameLabel)
                    .addComponent(UsernameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PasswordLabel)
                    .addComponent(passwordTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(ForbindKnap)
                .addContainerGap())
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 450, 370);
        getContentPane().add(jLabel1);
        jLabel1.setBounds(-7, -7, 430, 310);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ForbindKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ForbindKnapActionPerformed
        try {
            String[] stringArray = {HostTxt.getText(), PortNrTxt.getText(), DatabaseTxt.getText(), UsernameTxt.getText(), passwordTxt.getText()};
            FileHandler.writeFile("ini.txt", stringArray);
            System.out.println("Inifilen blev skrevet");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Kan ikke skrive til inifilen\n" + ex.getLocalizedMessage());
        }
        try {
            DatabaseHandler.getInstance().openConn("ini.txt");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1000 \nKunne ikke oprette forbindelse til serveren\nPrøv evt. igen senere. Serveren kan være nede." + ex.getLocalizedMessage(), "Databasetimeout", 0);
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1030 \nDriveren blev ikke fundet.\nKontakt systemadministratoren." + ex.getLocalizedMessage(), "Fejl", 0);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1050 \nKunne ikke skrive den nye ini-fil." + ex.getLocalizedMessage(), "Fejl", 0);
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1021 \nFejl ved læsning af inifilen. Login oplysninger i ini-filen er blevet ændret af en bruger.\n" + ex.getMessage(), "Fejl", 0);
        }


    }//GEN-LAST:event_ForbindKnapActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel DatabaseLabel;
    private javax.swing.JTextField DatabaseTxt;
    private javax.swing.JButton ForbindKnap;
    private javax.swing.JLabel HostLabel;
    private javax.swing.JTextField HostTxt;
    private javax.swing.JLabel OverskriftLabel;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JLabel PortNrLabel;
    private javax.swing.JTextField PortNrTxt;
    private javax.swing.JLabel UsernameLabel;
    private javax.swing.JTextField UsernameTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField passwordTxt;
    // End of variables declaration//GEN-END:variables
}
